package cn.com.qiancheng.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.qiancheng.dto.AdminDto;
import cn.com.qiancheng.dto.Student;
import cn.com.qiancheng.service.MD5;
import cn.com.qiancheng.service.PasswordService;

/**
 * Servlet implementation class UpdateP
 */
public class UpdatePServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String oldPassword = request.getParameter("oldPassword");
		oldPassword = MD5.GetMD5Code(oldPassword);
		String newP = request.getParameter("newPassword");
		newP = MD5.GetMD5Code(newP);
		String role = (String) request.getSession().getAttribute("role");
		if (role.equals("student")) {
			Student student = (Student) request.getSession().getAttribute("stu");
			String oldP = student.getStuPassword();
			if (oldP.equals(oldPassword)) {
				PasswordService ps = new PasswordService();
				student = ps.stupassUpdate(newP, student);
				response.sendRedirect("Login.html");
			} else {
				
			}
		} else if (role.equals("admin")) {
			AdminDto admin = (AdminDto)request.getSession().getAttribute("adm");
			String oldP = admin.getPassword();
			if(oldP.equals(oldPassword)){
				PasswordService ps = new PasswordService();
				admin = ps.adminpassUpdate(newP, admin);
				response.sendRedirect("Login.html");
			} else {
				
			}
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
